// subPages/communityService/communityShop/communityShop.js
const app = getApp();
let utils = require("../../../utils/util");
var loadMoreView,p = 1,num = 10,type='service'

Page({

  /**
   * 页面的初始数据
   */
  data: {
      themeColor:app.globalData.themeColor,
      btnText:'搜索',
      placeholderText:'输入您感兴趣的商品~',
      selectIndex:0,
      array:["好评优先","距离优先","人气优先",""],
      bannerSet:{
          interval:5000,    //自动切换时间间隔
          duration:500,     //滑动动画时间长短
          autoplay:true,    //是否自动切换默认false
          circular:true,    //是否衔接切换
          indicatorDots:true,     //显示指示点
          indicatorColor:'rgba(255,255,255,.8)', //指示点颜色
          indicatorActiveColor:app.globalData.themeColor //当前指示点颜色
      },
      bannerStyle:{
        width:"100%",  //banner图宽
        height:"200rpx" //banner图高
      },
      bannerList:[],
      iconNavList:[
          {
              background: '#00aeff',
              url:'/resources/communityService/icon06.png',
              iconW: 52,
              iconH: 46,
              text: '商城',
              index: 2,
              link:"/subPages/communityService/shop/shop"
          },
          {
              background: '#ff3d9d',
              url:'/resources/communityService/icon07.png',
              iconW: 52,
              iconH: 46,
              text: '二手市场',
              index: 3,
              link:"/subPages/communityService/secondHandMall/secondHandMall"
          }
      ],
      selectorItems:['好评优先','距离优先'],
      supermarketList:[]
  },
  bindPickerChange(e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
          selectIndex: e.detail.value
      })
  },



    iconNavBtn(e){
        let index = e.currentTarget.dataset.index;
        // console.log(index);
        wx.navigateTo({
            url:this.data.iconNavList[index].link
        })
    },
    goToSupermarket(e){
      let shopId = e.currentTarget.dataset.id;
        console.log(shopId);
        wx.navigateTo({
            url:'../shopContent/shopContent?id='+shopId
        })
    },
    //搜索页面跳转
    toSearchPage(){
      wx.navigateTo({
          url:'/subPages/communityService/searchPage/searchPage?type=1'
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    utils.setNavBarColor("#ffffff",app.globalData.themeColor);
    utils.setNavBarTitle("社区商城");
      loadMoreView = this.selectComponent("#loadMoreView")
      //type 塞选条件 1评分优先
    this.getBanner();
    this.getCommunityServiceShop()
  },
    getCommunityServiceShop(){
      var _this = this;
      utils.getAjax(`${app.globalData.apiUrl}/get_community_service_shop`,function(res){
          if(res.data.list.length != 0){
              var list = [];
              if(p == 1){
                  for(let i in res.data.list){
                      res.data.list[i].start = parseFloat(res.data.list[i].start)
                  }
                  list = res.data.list
              }else{
                  for(let i in res.data.list){
                      res.data.list[i].start = parseFloat(res.data.list[i].start)
                  }
                  list = that.data.supermarketList.concat(res.data.list)
              }
              _this.setData({
                  supermarketList:list
              })
          }else{
            _this.setData({
                supermarketList:[]
            })
          }
          loadMoreView.loadMoreComplete(res.data,type)
      },{
          type:1,
          community_id:app.globalData.communityId,
          p:p,
          num:num
      })

    },
    //获取服务数据异常
    shopDataErr(err){
        if(p>1){
            loadMoreView.loadMoreFail()
        }else{
            util.showModal('提示','获取商品失败，请向上滑动屏幕')
            this.setData({
                isFail: true
            })
        }
    },
    loadMoreListener: function(e) {
        p += 1
        this.getCommunityServiceShop()
    },

    clickLoadMore: function(e) {
        this.getCommunityServiceShop()
    },
    getBanner(){
      var that = this;
      utils.getAjax(`${app.globalData.apiUrl}/get_community_service_shop_banner`,res=>{
          // console.log(res);
          that.setData({
              bannerList:res.data
          })
      },{
          community_id: app.globalData.communityId
      })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      if(this.data.isFail){
          this.getCommunityServiceShop()
          return
      }
      loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
